// 所有couter组件的的业务逻辑都在这里进行管理
//逻辑更改数据

function todoReducer(data) {
  // console.log(data);
  //todoData: []

  function addItem(newItem) {
    // console.log(newItem);
    //{id: 1637677683568, text: '123', completed: false}

    // console.log(data.concat(newItem));
    //[{…}]

    return data.concat(newItem);
  }

  function removeItem(id) {
    return data.filter(item => item.id !== id);
  }

  function changeCompleted(id) {
    // console.log(id);
    //1637680007595

    // console.log(data);
    /**
     * [
     *   {
     *     completed: true,
     *     id: 1637680047784,
     *     text: "123"
     *   }
     * ]
     */

    return data.map((item) => {
      // console.log(item);
      if (item.id === id) {
        item.completed = !item.completed;
      }
    });
  }

  return {
    addItem,
    removeItem,
    changeCompleted
  }
}

export default todoReducer;